feat: token-aware output truncation for large tool responses#26
Conversation
Adds truncate.py with truncate_log_text() and truncate_list() helpers. Applies log truncation to dalgo_get_flow_run_logs. Closes #19. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
@claude can we review this PR? |
|
Claude finished @siddhant3030's task in 1m 43s —— View job Claude PR Review
Nice, well-scoped change. The head+tail strategy is the right call for logs, and emitting 🐛 Bugs / correctness1. half = max_lines // 2 # 0 when max_lines == 1
tail = lines[-half:] # lines[-0:] == lines[0:] == ALL linesIn Python, 2. Off-by-one when For 3. 4. Broad except Exception:
pass
return format_response(resp)If anything goes wrong inside the truncation block (e.g., 5. elif isinstance(data, dict) and "logs" in data:
result = truncate_log_text(str(data["logs"]))
data["logs"] = result["content"]
data["_meta"] = result["_meta"]
🤔 Design / framing6. "Token-aware" vs. line-aware. The PR title and module docstring say "token-aware," but the implementation counts lines, not tokens. A 100-line log of stack traces can still be tens of thousands of tokens. Two options:
7.
8. Inconsistent response shape across input types.
Models calling this tool will have to handle both. Consider normalizing to one shape (e.g., always 🧹 Nits
✅ What's good
If you want, I can push fixes for items 1–5 (the bugs) and item 7 (pull |
Resolves conflict in src/dalgo_mcp/tools/pipelines.py by combining: - adapt_context() pattern from PR #33 (replaces register(app, get_client) signature) - Token-aware log truncation in dalgo_get_flow_run_logs from this PR Both sets of changes are preserved: all tool files now use adapt_context() and dalgo_get_flow_run_logs still truncates large log responses using truncate_log_text() from src/dalgo_mcp/truncate.py. Also applies ruff formatting fixes to context.py, docs.py, and truncate.py. https://claude.ai/code/session_011MeXGRNe15ksjbwsDM8ULN
74ebf36 to
034d838
Compare
…cation # Conflicts: # src/dalgo_mcp/server.py # src/dalgo_mcp/tools/pipelines.py
Summary
src/dalgo_mcp/truncate.pywithtruncate_log_text()andtruncate_list()dalgo_get_flow_run_logs(head+tail, 100 lines default)_metawith total/shown/omitted counts and a noteWhy
Pipeline logs can be megabytes. Without truncation, a single
dalgo_get_flow_run_logscall can consume most of the context window, leaving no room for the model to reason about the result.Test plan
dalgo_get_flow_run_logson a large log returns max 100 lines_meta.truncated=trueand_meta.omittedcount present when truncated_meta.truncated=falsetruncate_log_text("")handles empty input gracefullyCloses #19
🤖 Generated with Claude Code